<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .ad{
            position: fixed;
            right: 0;
            bottom: 0;
            left: 0;
            top: 0;
            margin: auto;
            width: 200px;
            height: 200px;
            background: red;
            /* display: none ; */
            visibility: hidden;
        }
        .ad > span {
            float: right;
            width: 30px;
            height: 30px;
            background-color: #ff0;
            text-align: center;
            line-height: 30px;
            /* 加个手 */
            cursor: pointer;
        }
    </style>
</head>
<body>

    <div class="ad" id="ad">
        <span id="close">X</span>
        <h2>显示当前的时间</h2>
        <h3>距离清明节还有</h3>
        <p>
            <span id="show">10</span>
            s之后自动关闭
        </p>
    </div>

    <script>


        // 延时器改成定时器   ----  递归

        // 定时器 叠加   及时清除

        // 异步！！！！！！




        var oSpan = document.getElementById('show') ;
        var oAd = document.getElementById('ad') ;
        var oClose = document.getElementById('close') ;

        ad() ;

       
        function ad() {  
            setTimeout(function () {  
            // 显示小广告
                oAd.style.visibility = 'visible' ;
                var count = 10 ;
                oSpan.innerHTML = count ;

                // 点击X 
                oClose.onclick = function () {  
                    oAd.style.visibility = 'hidden' ;
                    clearInterval(t) ;
                    
                    ad() ;
                }

                // 自动关闭
                
                var t = setInterval(function () {  
                    count-- ;
                    oSpan.innerHTML = count ;
                    if(count <= 0) {
                        clearInterval(t) ;
                        oAd.style.visibility = 'hidden' ;
                        ad() ;
                    }
                } , 400)



            } , 2000)
        }

        
    </script>
    
</body>
</html>